package com.zmc.admin.controller.login;

import com.zmc.admin.application.LoginApplicationService;
import com.zmc.admin.infrastructure.bean.vo.identity.MerchantInfoVo;
import com.zmc.admin.infrastructure.bean.vo.identity.VerifyIdentityVo;
import com.zmc.admin.infrastructure.bean.vo.login.LoginVo;
import com.zmc.admin.infrastructure.bean.vo.login.RegisterVo;
import com.zmc.admin.infrastructure.bean.vo.login.SendCodeVo;
import com.zmc.admin.infrastructure.bean.vo.login.VerifyPhoneVo;
import com.zmc.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * 登录验证
 *
 * @author zmc
 */
@RestController
public class SysLoginController {
    @Autowired
    private LoginApplicationService loginApplicationService;

    /**
     * 注册
     * */
    @PostMapping("/register")
    public R register(@Validated @RequestBody RegisterVo user) {
        loginApplicationService.register(user);
        return R.ok();
    }

    /**
     * 登录
     *
     * @param loginVo 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public R login(@Validated @RequestBody LoginVo loginVo) {
        // 生成令牌
        Map<String, Object> map = loginApplicationService.login(loginVo.getUsername(), loginVo.getPassword(), loginVo.getCode(),
                loginVo.getUuid());
        return R.ok().put(map);
    }

    /**
     * 发送验证码
     */
    @PostMapping("/send/sms")
    public R sendSms(@Validated @RequestBody SendCodeVo vo) {
        loginApplicationService.sendSms(vo.getPhone());
        return R.ok();
    }

    /**
     * 验证 验证码
     */
    @PostMapping("/confirm/sms")
    public R confirmSms(@Validated @RequestBody VerifyPhoneVo vo) {
        loginApplicationService.verify(vo);
        return R.ok();
    }

    /**
     * 身份认证
     */
    @PostMapping("/identity/auth")
    public R identityAuth(@Validated @RequestBody VerifyIdentityVo vo) {
        loginApplicationService.identityAuth(vo);
        return R.ok();
    }

    /**
     * 商户信息录入
     */
    @PostMapping("/merchant/info")
    public R merchantInfo(@Validated @RequestBody MerchantInfoVo vo) {
        loginApplicationService.enterMerchantInfo(vo);
        return R.ok();
    }

    @PostMapping("/logout")
    public R logout() {
        loginApplicationService.logout();
        return R.ok();
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @PostMapping("getInfo")
    public R getInfo() {
        return R.ok(loginApplicationService.getUserInfo());
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @PostMapping("getRouters")
    public R getRouters() {
        return R.ok(loginApplicationService.getRouters());
    }
}
